// pages/logs/order/details/details.js by_class 
const app = getApp()
const connector = require('../../../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    orderOnes:{},
    types:'',//1=>从商品下单进入（取消付款）2=>从订单列表进入

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    orderDetail(that, JSON.parse(options.queryBean))

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.hideShareMenu()
  },
  orderAnew(e){
    
  },
  /**支付 */
  orderPay(e){
    var that = this;
    var cartNew = [];
    preOrder(that, that.data.orderOnes.order_id)
  
  },
  /**取消订单 */
  orderCancel(e){
    var that = this;
    // let detailsid = that.data.orderOnes.data.id;
    
    wx.showModal({
      title: "取消订单",
      content: "您确定取消订单吗？",
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          orderCancel(that)
        }
      }
    })
  },
  //再来一单
  orderAnew(e){
    let that = this;
    let headline = that.data.orderOnes.snap_items
    let arr = []
    for (let i = 0; i < headline.length;i++){
      let shopping = {
        id: headline[i].id,
        // category_id: headline.category_id,
        main_img_url: headline[i].main_img_url,
        name: headline[i].name,
        price: headline[i].price,
        send_type: headline[i].send_type
      }
      arr.push(shopping)
    }
    wx.setStorageSync('headlineBuy', arr)
    wx.setStorageSync('buy_num', 2)
    wx.navigateTo({
      url: '/pages/index/shopCart/accounts/accounts',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
/**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})
//获取订单详情
function orderDetail(that, orderSn) {
  connector.getFunc(connector.connector().orderDetail + orderSn, "", "get", app.tokens, function (res) {

    if (res.statusCode == 200) {
      that.setData({
        orderOnes: res.data
      })

    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//取消订单
function orderCancel(that) {
  const cancelOrder = {
    order_no: that.data.orderOnes.order_sn
  }
  connector.getFunc(connector.connector().orderCancel, cancelOrder, "post", app.tokens, function (res) {

    if (res.statusCode == 200) {
      if (res.data.result){
        wx.showToast({
          title: "取消成功",
          icon: 'none',
          duration: 1000
        })
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },1000)
      }
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}
//支付
function preOrder(that, orderId){
  wx.request({
    url: connector.connector().conuser + '/pay/pre_order',
    method: 'POST',
    header: {
      token: app.tokens
    },
    data: {
      id: orderId
    },
    success: function (ser) {
      var preData = ser.data;
      //拉取微信支付
      wx.requestPayment({
        timeStamp: preData.timeStamp.toString(),
        nonceStr: preData.nonceStr,
        package: preData.package,
        signType: preData.signType,
        paySign: preData.paySign,
        success: function (sss) {
          // wx.navigateTo({
          //   url: '/pages/oneself/order/order',
          // })
          wx.navigateBack({
            delta: 1,
          })
        },
        fail: function (error) {
          // wx.navigateTo({
          //   url: '/pages/oneself/order/order',
          // })
          // wx.navigateTo({
          //   url: '/pages/oneself/order/details/details?queryBean=' + JSON.stringify(res.data.order_no),
          // })
        }

      })

    }
  })
}